package com.zablert.mainactivity.bezier

import android.content.Context
import android.graphics.PointF
import android.os.Bundle
import android.view.View
import android.widget.CompoundButton
import android.widget.RadioGroup
import com.alibaba.android.arouter.facade.annotation.Route
import com.zablert.mainactivity.R
import com.zablert.mainactivity.databinding.ActivityBezierBinding
import zyc.zalbert.activity.CoreBVMActivity
import zyc.zalbert.extensions.logd
import zyc.zalbert.router.RouteConstants

@Route(path = RouteConstants.TEST_FRAGMENT_CONTAINER)
class BezierActivity : CoreBVMActivity<ActivityBezierBinding,BezierModel>() {

    override fun layoutId() = R.layout.activity_bezier

    override fun initViewModel(): BezierModel {
        return BezierModel()
    }

    override fun onInitContent() {
        super.onInitContent()
        binding.circleBezierView.setIsShowHelpLine(true)
        binding.showLineSwitch.setChecked(true)
        binding.rgStatus.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
            if (checkedId == R.id.status_free) {                // 自由变换
                binding.circleBezierView.setStatus(BezierView.Status.FREE)
            } else if (checkedId == R.id.status_mirror_diff) {  // 镜像异向
                binding.circleBezierView.setStatus(BezierView.Status.MIRROR_DIFF)
            } else if (checkedId == R.id.status_three) {        // 三点拽动
                binding.circleBezierView.setStatus(BezierView.Status.THREE)
            } else if (checkedId == R.id.status_mirror_same) {  // 镜像同向
                binding.circleBezierView.setStatus(BezierView.Status.MIRROR_SAME)
            }
        })

        binding.showLineSwitch.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
            binding.circleBezierView.setIsShowHelpLine(
                isChecked
            )
        })

    }

    fun onReset(view: View?) {
        binding.circleBezierView.reset()
    }

    fun onLog(view: View?) {
        val controlPointList: List<PointF> = binding.circleBezierView.getControlPointList()
        val stringBuilder = StringBuilder()
        stringBuilder.append("\n")
        for (i in controlPointList.indices) {
            stringBuilder.append("第")
                .append(i)
                .append("个点坐标(单位dp)：[")
                .append(px2dip(this, controlPointList[i].x))
                .append(", ")
                .append(px2dip(this, controlPointList[i].y))
                .append("]")
                .append("\n")
        }
        "控制点日志: $stringBuilder".logd(this)
    }

    fun px2dip(context: Context, pxValue: Float): Int {
        val density: Float = context.getResources().getDisplayMetrics().density
        return (pxValue / density + 0.5f).toInt()
    }
}